﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestGraphicEnviroment
{
	public partial class Form1 : Form
	{

		private bool allowPaint = false;
		private Bitmap bitmap = null;
		
		private int grid = 20;

		public Form1()
		{
			InitializeComponent();
			this.bitmap = new Bitmap( this.pnlContent.Width, this.pnlContent.Height );
		}

		private void pnlContent_MouseUp( object sender, MouseEventArgs e )
		{
			this.allowPaint = false;
		}

		private void pnlContent_MouseDown( object sender, MouseEventArgs e )
		{
			allowPaint = true;
			Point pos = new Point( grid * ( ( e.X + grid / 2 ) / grid ), grid * ( ( e.Y + grid / 2 ) / grid ) );
			this.DrawSquare(pos);
			
		}

		private void btnClear_Click( object sender, EventArgs e )
		{
			Graphics g = this.pnlContent.CreateGraphics();
			g.Clear( this.pnlContent.BackColor );
			g.Dispose();
		}

		private void pnlContent_MouseMove( object sender, MouseEventArgs e )
		{
			if( allowPaint )
			{
				Point pos = new Point( grid * ( ( e.X + grid / 2 ) / grid ), grid * ( ( e.Y + grid / 2 ) / grid ) );
				this.DrawSquare(pos);
			}
		}


		private void DrawSquare(Point pos)
		{
			
			Graphics g = this.pnlContent.CreateGraphics();

			g.FillRectangle( new SolidBrush( Color.Black ), pos.X, pos.Y, grid, grid );
			g.Dispose();
		}

		private void toolStripButton1_Click( object sender, EventArgs e )
		{
			Graphics g = this.pnlContent.CreateGraphics();
			System.Drawing.Drawing2D.GraphicsState state = g.Save();

			this.pnlContent.DrawToBitmap( this.bitmap, new Rectangle( 0, 0, this.bitmap.Width, this.bitmap.Height ) );
			
			
			//this.bitmap.Dispose();
			//this.pictureBox1.Image = this.bitmap;
			List<Point> p = Solver.Step1( this.bitmap );
		}											
	}
}
